package yunquan.wang.modules.base.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import yunquan.wang.base.dao.PropertyFilter;
import yunquan.wang.base.web.WebConstants;
import yunquan.wang.base.web.WebHelper;
import yunquan.wang.base.web.ajax.AjaxDone;
import yunquan.wang.modules.base.entity.CodeType;
import yunquan.wang.modules.base.entity.CodeValue;
import yunquan.wang.modules.base.service.AppCodeService;

@Controller
@RequestMapping("/core/appcode")
public class AppcodeController{

@Resource
private	AppCodeService service;
 
 @RequestMapping(value = "/list.do")
 public String list(HttpServletRequest request, Model model) {
	Pageable page = WebHelper.getPage(request);
	List<PropertyFilter> filters = WebHelper.filterRequest(request);
	model.addAttribute(WebConstants.ATTRIBUTE_PAGE,service.findPage(page, filters));
		return "core/codeTypeList";
	}

	/**
	 * 保存
	 * @param entity
	 * @param bindingResult
	 * @return
	 */
 
	@RequestMapping(value = "/save.do")
	public @ResponseBody AjaxDone save (@Valid CodeType entity,	BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return WebHelper.validFail(bindingResult);
		} else {
			try {
				entity = service.save(entity);
			} catch (Exception e) {
			return	WebHelper.saveFail( e);
			}
		}
		return WebHelper.saveSuccess();
	}

 
	/**
	 * 输入更改
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/input.do")
	public String input(String id,Model model) {
		CodeType entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = service.get(id);
		} else {
			entity = new CodeType();
		}
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, entity);
		return "core/codeTypeInput";
	}
	/**
	 * 删除码表类型
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete.do")
	public @ResponseBody AjaxDone delete(String [] id) {
		for(String ids:id){
		service.delete(ids);
		}
	return	WebHelper.deleteSuccess();
	}
	
	/**
	 * 码表列表
	 * @param model
	 * @param typeId
	 * @return
	 */
	@RequestMapping(value = "/codelist.do")
	public String codeList(Model model,String id) {
		 List<CodeValue> listCode=	service.getCodeValues(id);
		 model.addAttribute("listCode",listCode );
		 model.addAttribute("typeId", id);
		return "core/codelist";
	}
 
	@RequestMapping(value = "/codeinput.do")
	public String editCode(String codeTypeId,String id,Model model)
	{

		CodeValue entity;
		if (StringUtils.isNotBlank(id)) {
			entity = service.getCodeValue(id);
		} else {
			entity = new CodeValue();
			entity.setEnable(true);
		}
		CodeType codetype = new CodeType();
		codetype.setId(codeTypeId);
		entity.setCodeType(codetype);
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, entity);
		return "core/codeinput";
		
	}
	
	@RequestMapping(value = "/codesave.do")
	public @ResponseBody  AjaxDone  saveCode(CodeValue entity,BindingResult bindingResult)
	{
		if (bindingResult.hasErrors()) {
			return WebHelper.validFail(bindingResult);
		} else {
			try {
				entity = service.saveCodeValue(entity);
			} catch (Exception e) {
			return	WebHelper.saveFail( e);
			}
		}
		return WebHelper.saveSuccess();
	 
	}

	@RequestMapping(value = "/codedelete.do")
	public @ResponseBody AjaxDone deletecode(String [] id) {
		for(String ids:id){
		service.delete(ids);
		}
		return	WebHelper.deleteSuccess();
	}
	
 
 
}
